package com.itboye.mengwu.widget;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.itboye.mengwu.R;
import com.itboye.mengwu.address.CityModel;
import com.itboye.mengwu.address.ProvinceModel;

/**
 * 
 * @author 在数据库中根据邮编查询地区地址
 * @since 2016-07-29
 * 
 */
public class BySqlUtil {

	// 数据库
	private SQLiteDatabase db;

	List<ProvinceModel> provinceLv = new ArrayList<ProvinceModel>();
	List<CityModel> cityLv = new ArrayList<CityModel>();
	private String DB_PATH;// =App.ctx.getCacheDir().getCanonicalPath() +
							// "/address";
	private final String DB_FILENAME = "itboye.db";

	String provinceName, cityName;

	public String getProvinceName() {
		return provinceName;
	}

	public String getCityName() {
		return cityName;
	}

	/**
	 * 
	 * @param provinceNo
	 *            省份名称
	 * @param cityNo
	 *            城市名称
	 * @param context
	 */
	public void getProviceName(String provinceNo, String cityNo, Context context) {

		try {
			DB_PATH = context.getCacheDir().getCanonicalPath() + "/address";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		db = openDataBase(context);

		Cursor cursorCity = db.rawQuery("select * from common_city", null);
		while (cursorCity.moveToNext()) {
			CityModel cityModel = new CityModel(cursorCity.getString(2),
					cursorCity.getInt(1), cursorCity.getInt(3));
			cityLv.add(cityModel);
		}
		Log.e("---!1", cityNo);
		for (int i = 0; i < cityLv.size(); i++) {

			if (cityNo.equals(cityLv.get(i).getNumber() + "")) {
				cityName = cityLv.get(i).getName() + "";
				Log.e("---1", cityNo);
				Log.e("---2", cityLv.get(i).getNumber() + "");
				Log.e("---3", cityName);
			}
		}
		cursorCity.close();

		Cursor cursor = db.rawQuery("select * from common_province", null);
		while (cursor.moveToNext()) {
			ProvinceModel province1 = new ProvinceModel(cursor.getString(2),
					cursor.getInt(1));
			provinceLv.add(province1);
		}

		// mProvinceDatas = new String[provinceLv.size()];
		for (int i = 0; i < provinceLv.size(); i++) {

			if (provinceNo.equals(provinceLv.get(i).getNumber() + "")) {
				provinceName = provinceLv.get(i).getName();
			}
		}
		cursor.close();

		db.close();

	}
/**
 * 打开数据库
 * @param context
 * @return
 */
	public SQLiteDatabase openDataBase(Context context) {
		try {
			String dbFileName = DB_PATH + "/" + DB_FILENAME;
			File dir = new File(DB_PATH);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			if (!new File(DB_PATH, DB_FILENAME).exists()) {
				new File(DB_PATH, DB_FILENAME).createNewFile();
				InputStream is = context.getResources().openRawResource(
						R.raw.itboye);
				FileOutputStream fos = new FileOutputStream(dbFileName);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}
				fos.close();
				is.close();
			}
			db = SQLiteDatabase.openOrCreateDatabase(dbFileName, null);
			return db;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
